/**
 * Created by wdj on 16/9/6.
 * action
 */

function makeAction (type) {
    return ({ dispatch }, ...args) => dispatch(type, ...args)
}

const initNote = {
    id: +Date.now(),
    title: '我的笔记',
    content: '第一篇笔记内容',
    favorite: false
}

const initData = {
    show: 'all',
    notes: [initNote],
    activeNote: initNote
}

export const initStore = ({ dispatch }) => {
    setTimeout(function () {
        let notesData = window.localStorage['notesData']
        if (notesData && notesData[0] === '{') {
            notesData = JSON.parse(notesData)
        }
        console.log(notesData)
        dispatch('INIT_STORE', notesData || initData)
    }, 10)
}

// 更新当前activeNote对象
export const updateActiveNote = makeAction('SET_ACTIVE_NOTE')

// 添加一个note对象
export const newNote = makeAction('NEW_NOTE')

// 删除一个note对象
export const deleteNote = makeAction('DELETE_NOTE')
export const toggleFavorite = makeAction('TOGGLE_FAVORITE')
export const editNote = makeAction('EDIT_NOTE')

// 更新列表展示
export const updateShow = makeAction('SET_SHOW_ALL')

